module sub_mon(
	clk,
	rst,
	ivld,
	din
	);

parameter string FILE =  "./din.txt" ;
parameter   WIDTH =   16 ; 

input   clk                ;
input   rst  	           ;
input   ivld   		   ;
input   [WIDTH-1 :0]   din ;

integer  fp_r ;
integer  count      ;
reg     [WIDTH - 1:0]        RDATA;

initial begin 
	fp_r = $fopen(FILE,"r");
	count = $fscanf(fp_r,"%b",RDATA);
end


reg    [31:0]      icnt  ;

reg  err_flag  ;
always @ (posedge clk  or posedge rst) 
begin 
 if (rst) begin
	 err_flag  <= 'd0 ;
	 icnt      <=  'd0 ;
    end
    else if (ivld) begin
	    icnt  <=  icnt + 'd1 ;
	    if (  (RDATA == din)  ||  (RDATA == din+1) ||  (RDATA == din-1)  ) begin
		   err_flag  <= 'd0 ; 
	    end
	    else begin 
		    err_flag  <= 'd1 ;
		    $display("%m:the %d  data compare fail!!!! reference and rtl  is %h --  %h \n" , icnt  ,  RDATA  , din );
	    end
	#1
	count = $fscanf(fp_r,"%b",RDATA);	    
    end
    else begin 
	 err_flag  <= 'd0 ;
    end
end

always @ (posedge clk  ) 
begin 
     if ( err_flag ) begin
	     #1000
	     $display("%m: CASE_FAILE  !!!!!  \n");
	     $finish();
     end
end





endmodule




